﻿$.validator.methods.equal = function (value, element, param) {
    return value == param;
};

$().ready(function () {

    $("#optHardTissue").attr("class", "ListSelectedOption");
    $("#optDashboard").attr("class", "");
    $("#optGeneral").attr("class", "current");
    $("#optAccounting").attr("class", "");

    var validator = $("#frmHardTissue").bind("invalid-form.validate", function () {
        $("#lblErrorMsgHardTissue").show();
    }).validate({
        debug: true,
        rules: {
            txtHardTissueAlterations: {
                required: true,
                minlength: 5
            }
        },
        messages: {
            txtHardTissueAlterations: ""
        }
    });

    $('#btnClearHardTissueForm').live('click', function () {
        ClearHardTissueForm();
    });

    $('#btnAddHardTissue').live('click', function () {
        IsValidHardTissueForm();
    });

    $("#lblErrorMsgHardTissue").hide();
    $("#lblsuccessMsgHardTissue").hide();

    $("#txtHardTissueAlterations").removeClass("error");
    $("#chkVisibleHardIssue").removeClass("error");
});

function IsValidHardTissueForm() {
    if ($("#frmHardTissue").valid()) {
        $("#lblErrorMsgHardTissue").hide();
        $("#lblsuccessMsgHardTissue").hide();
        AddHardTissue();
    }
}

function AddHardTissue() {
    $.ajax(
    {
        type: "POST",
        url: "/ODSYSUI/General/AddHardTissue",
        data:
            JSON.stringify({
                Alterations: $('#txtHardIssueAlterations').val(),
                Visible: $('#chkVisibleHardTissue').is(':checked')
            }),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (result) {
            if (result.success) {
                ClearHardTissueForm();
                $("#lblsuccessMsgHardTissue").show(300).delay(5000).hide(400);
            }
            else {
                $("#lblsuccessMsgHardTissue").hide();
                $("#lblErrorMsgHardTissue").show();
                $("#frmHardTissueErrorMessage").text(result.msg);
            }
        },
        error: function (req, status, error) {
            $("#lblsuccessMsgHardTissue").hide();
            $("#lblErrorMsgHardTissue").show();
            $("#frmHardTissueErrorMessage").text("An error has occurred. Please contact your administrator.");
        }
    });
}

function ClearHardTissueForm() {
    $('#txtHardTissueAlterations').val('');
    $('#chkVisibleHardTissue').attr('checked', false);

    $("#lblErrorMsgHardTissue").hide();
    $("#lblsuccessMsgHardTissue").hide();

    $("#txtHardTissueAlterations").removeClass("error");
    $("#chkVisibleHardIssue").removeClass("error");
}